package com.winai.launcher.view.fragment;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.othershe.calendarview.bean.DateBean;
import com.othershe.calendarview.listener.OnPagerChangeListener;
import com.othershe.calendarview.listener.OnSingleChooseListener;
import com.othershe.calendarview.utils.CalendarUtil;
import com.othershe.calendarview.weiget.CalendarView;
import com.winai.launcher.R;
import com.winai.launcher.base.BaseActivity;
import com.winai.launcher.constant.MainRouter;
import com.winai.launcher.override.LazyFragmentPagerAdapter;
import com.winai.launcher.utils.OpenAppUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：康少
 * 时间：2019/12/12 0012
 * 说明：日历——fragment
 */
@SuppressLint("SetTextI18n")
public class CalendarFragment extends Fragment implements LazyFragmentPagerAdapter.Laziable {
    private Context mContext;

    @BindView(R.id.pb_loading)
    ProgressBar pbLoading;
    @BindView(R.id.str_title)
    TextView strTitle;
    @BindView(R.id.tv_yearMonth)
    TextView tvYearMonth;
    @BindView(R.id.calendar)
    CalendarView calendarView;
    @BindView(R.id.view)
    View view;

    private int[] cDate = CalendarUtil.getCurrentDate();

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        this.mContext = context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_calendar, container, false);
        ButterKnife.bind(this, inflate);
        showProgressBar();
        start();
        return inflate;
    }

    private void start() {
        initObject();
        initListener();
        new Handler().postDelayed(new Thread(new Runnable() {
            @Override
            public void run() {
                calendarView.setVisibility(View.VISIBLE);
                view.setVisibility(View.VISIBLE);
            }
        }), 1000);
//        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                calendarView.setVisibility(View.VISIBLE);
//            }
//        }, 500);
    }

    private void initObject() {
        strTitle.setText("日历");
        calendarView
//                .setSpecifyMap(map)
                .setStartEndDate("2018.1", "2049.12")
//                .setDisableStartEndDate("2018.1.1", "2049.12.31")
                .setInitDate(cDate[0] + "." + cDate[1])
                .setSingleDate(cDate[0] + "." + cDate[1] + "." + cDate[2])
                .init();
        tvYearMonth.setText(cDate[0] + "年" + cDate[1] + "月");
    }

    private void initListener() {
        calendarView.setOnPagerChangeListener(new OnPagerChangeListener() {
            @Override
            public void onPagerChanged(int[] date) {
                tvYearMonth.setText(date[0] + "年" + date[1] + "月");
            }
        });

        calendarView.setOnSingleChooseListener(new OnSingleChooseListener() {
            @Override
            public void onSingleChoose(View view, DateBean date) {
                tvYearMonth.setText(date.getSolar()[0] + "年" + date.getSolar()[1] + "月");
            }
        });
        calendarView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (calendarView.getVisibility() == View.VISIBLE) {
                    hidingProgressBar();
                }
            }
        });
    }

    public void showProgressBar() {
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                pbLoading.setVisibility(View.VISIBLE);
//            }
//        }).start();
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                pbLoading.setVisibility(View.VISIBLE);
            }
        });
    }

    public void hidingProgressBar() {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                pbLoading.setVisibility(View.GONE);
            }
        });
    }

    @OnClick({R.id.iv_back, R.id.iv_home, R.id.iv_left, R.id.iv_right, R.id.cl_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                ((BaseActivity) mContext).finish();
                break;
            case R.id.iv_home:
                //模拟HOME
                Intent it = new Intent(Intent.ACTION_MAIN);
                it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                it.addCategory(Intent.CATEGORY_HOME);
                mContext.startActivity(it);
                break;
            case R.id.iv_left:
                calendarView.lastMonth();
                break;
            case R.id.iv_right:
                calendarView.nextMonth();
                break;
            case R.id.cl_search:
                OpenAppUtils.startIntentPath(mContext, MainRouter.PATH_FORUM_SEARCH, ".搜索@");
                break;
        }
    }
}
